package com.cloudsdo.cameratest.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.cloudsdo.cameratest.R;
import com.cloudsdo.cameratest.activity.BaseActivity;
import com.cloudsdo.cameratest.dao.Student;
import com.cloudsdo.cameratest.dao.StudentDao;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class RegisterActivity extends BaseActivity {

    private ImageView imvHead;
    private EditText tvName;
    private TextView tvLog;
    private StudentDao dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        imvHead=findViewById(R.id.imvHead);
        tvName=findViewById(R.id.tvName);
        tvLog=findViewById(R.id.tvLog);
        imvHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goCamera();
            }
        });
        dao = new StudentDao(getContext(), Student.class);
    }

    private File cameraSavePath;//拍照照片路径
    private Uri uri;//照片uri

    //激活相机操作
    private void goCamera() {
        try{
            File root=new File(Environment.getExternalStorageDirectory().getPath() + "/faceimg/");
            if(!root.exists()){
                root.mkdirs();
            }
            cameraSavePath = new File(root,System.currentTimeMillis() + ".jpg");
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            try {
                if (cameraSavePath.exists()) {
                    cameraSavePath.delete();
                }
                cameraSavePath.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (cameraSavePath != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    //如果是7.0及以上的系统使用FileProvider的方式创建一个Uri
                    Log.e(TAG, "Build.VERSION.SDK_INT >= Build.VERSION_CODES.N");
                    uri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", cameraSavePath);
                    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                } else {
                    //7.0以下使用这种方式创建一个Uri
                    uri = Uri.fromFile(cameraSavePath);
                }
            }
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            startActivityForResult(intent, 1);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    String photoPath="";
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                photoPath = String.valueOf(cameraSavePath);
            } else {
                photoPath = uri.getEncodedPath();
            }
            Log.d("拍照返回图片路径:", photoPath);
            Glide.with(getContext()).load(photoPath).into(imvHead);
        }
    }

    public void register(View view) {
        String name=tvName.getText().toString();
        if( TextUtils.isEmpty(name)){
            Toast.makeText(getContext(),"请填写完善",Toast.LENGTH_SHORT).show();
            return;
        }
        Student student=new Student();
        student.setName(name);
        student.setImg(photoPath);
        dao.insert(student);

        List<Student> list=dao.queryAll();
        StringBuilder stringBuilder=new StringBuilder();
        for(int i=0;i<list.size();i++){
            Student item=list.get(i);
            stringBuilder.append(item.toString());
        }
        tvLog.setText(stringBuilder.toString());
    }
}
